package com.be.beadmin.material.controller;

import com.alibaba.excel.EasyExcel;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.utils.ResultModel;
import com.be.beadmin.material.dto.input.PurchaseOrderDetailDto;
import com.be.beadmin.material.dto.output.PurchaseOrderDetailEx;
import com.be.beadmin.material.service.PurchaseOrderDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
* @author zmy
* @date 2023-05-14
**/
@Api(tags = {"物料管理"})
@RestController
public class PurchaseOrderDetailController {

    @Autowired
    private PurchaseOrderDetailService purchaseOrderDetailService;


    @PostMapping(value = "/material/getPurchaseOrderDetailByPage")
    public ResultModel getPurchaseOrderDetailByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(purchaseOrderDetailService.getPurchaseOrderDetailByPage(pageEntity)) ;
    }

    @ApiOperation("查询订单+Detail")
    @PostMapping(value = "/material/getPurchaseOrderTotalByPage")
    public ResultModel getPurchaseOrderTotalByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(purchaseOrderDetailService.getPurchaseOrderTotalByPage(pageEntity)) ;
    }
    @ApiOperation("新建详细物料，POId...对应主表的id")
    @PostMapping("/material/addPurchaseOrderDetail")
    public ResultModel addPurchaseOrderDetail(@RequestBody PurchaseOrderDetailDto purchaseOrderDetailDto){
        purchaseOrderDetailService.addPurchaseOrderDetail(purchaseOrderDetailDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @PostMapping("/material/delPurchaseOrderDetail")
    public ResultModel delPurchaseOrderDetail(@RequestBody PurchaseOrderDetailDto purchaseOrderDetailDto){
        purchaseOrderDetailService.delPurchaseOrderDetail(purchaseOrderDetailDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }


    @PostMapping("/material/updatePurchaseOrderDetail")
    public ResultModel updatePurchaseOrderDetail(@RequestBody PurchaseOrderDetailDto purchaseOrderDetailDto){
        purchaseOrderDetailService.updatePurchaseOrderDetail(purchaseOrderDetailDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/material/exportPurchaseOrderDetail")
    public void exportPurchaseOrderDetail(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<PurchaseOrderDetailEx> purchaseOrderDetailPage = purchaseOrderDetailService.getPurchaseOrderDetailByPage(pageEntity);
        List<PurchaseOrderDetailEx> purchaseOrderDetailExes = purchaseOrderDetailPage.getContent();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("PurchaseOrderDetail接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), PurchaseOrderDetailEx.class).sheet("sheet1").doWrite(purchaseOrderDetailExes);
    }

    @ApiOperation("分料（一种）")
    @PostMapping("/material/setMaterialNum")
    public ResultModel setMaterialNum(@ApiParam("主键id") @RequestParam String id,
                                      @ApiParam("交付的数量" ) @RequestParam int num){
        purchaseOrderDetailService.setMaterialNum(id,num);
        return ResultModel.success();
    }
}